package com.bank.bank.account.service.impl;

import com.bank.bank.account.client.TransactionClient;
import com.bank.bank.account.mapper.AccountMapper;
import com.bank.bank.account.service.AccountService;
import com.bank.bank.common.base.pojo.dto.AccountAddParam;
import com.bank.bank.common.base.pojo.dto.AccountDTO;
import com.bank.bank.common.base.pojo.entity.Account;
import com.bank.bank.common.base.pojo.entity.Transaction;
import com.bank.bank.common.base.pojo.vo.AccountTransactionVO;
import com.bank.bank.common.base.pojo.vo.AccountUpdateVO;
import com.bank.bank.common.base.response.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Slf4j
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    //注入openFeign客户端
    @Autowired
    private TransactionClient transactionClient;
    /**
     * 1.添加账户业务
     * @param accountAddParam
     * @return
     */
    @Override
    public Account addAccount(AccountAddParam accountAddParam) {
        log.debug("添加账户业务:accountAddParam:{}", accountAddParam);
        Account account = new Account();
        BeanUtils.copyProperties(accountAddParam, account);
        //设置余额为:0.00
        account.setBalance(0.00);
        account.setCreatedAt(new Date());
        //处理卡号: 时间戳 + 四位随机数[1000-9999];
        long timestamp = System.currentTimeMillis();
        int random = new Random().nextInt(9000) + 1000; //1000-9999
        String accountNumber = "" + timestamp + random;
        account.setAccountNumber(accountNumber);
        //调用接口,插入数据
        accountMapper.insertAccount(account);
        //id已处理: <insert id="insertAccount" useGeneratedKeys="true" keyProperty="id">
        return account;
    }

    /**
     * 2.查询账户列表业务
     * @return
     */
    @Override
    public List<Account> listAccounts(Integer customerId) {
        List<Account> accountList =null;
        if (customerId==null){
            accountList=accountMapper.selectAllAccount();
        }else{
            accountList=accountMapper.selectCustomerAccount(customerId);
        }
        return accountList;
    }

    /**
     * 3.注销账户业务
     * @param accountId
     */
    @Override
    public void delAccount(Long accountId) {
        log.debug("注销账户业务:accountId:{}", accountId);
        accountMapper.deleteAccountById(accountId);
    }

    /**
     * 4.查询账户交易详情
     * @param accountId 账户id
     * @return
     */
    @Override
    public AccountTransactionVO getAccountTransactions(Integer accountId) {
        log.debug("查询账户交易详情业务:accountId:{},当前交易客户端:{}", accountId,transactionClient.getClass().getName());
        /*AccountTransactionVO accountTransactionVO = accountMapper.selectAccountTransactionByAccountId(accountId);*/
        JsonResult accountTrans = transactionClient.getAccountTrans(accountId);
        log.debug("accountTrans:{}", accountTrans);
        List<Transaction> datas= (List<Transaction>) accountTrans.getData();
        log.debug("交易记录为:{}",datas);
        //封装
        AccountTransactionVO vo=new AccountTransactionVO();
        vo.setAccountId(accountId);
        vo.setTransactions(datas);
        return vo;
    }

    /**
     * 1. 根据accountId 查询账户Account select * from account where id=?
     * 2. 更新最新的balance update account set balance=? where id=? 业务层算最新
     * 区别 update account set  balance=balance+? where id=? 数据库算
     * 根据结果 封装返回vo
     */
    @Override
    public AccountUpdateVO updateAndGetAccount(Integer accountId, Double balance) {
        //1.查询账户
        Account account=accountMapper.selectAccountById(accountId);
        //2.严谨一点要判断非空 查不到 没法更新
        if (account!=null){
            account.setBalance(account.getBalance()+balance);
            //有一种entity更新的持久层规范 习惯 更新传如的对象 非空属性是更新字段
            accountMapper.updateAccountBalance(account);
            //3 组织一个vo
            AccountUpdateVO vo=new AccountUpdateVO();
            vo.setToAccountNumber(account.getAccountNumber());
            vo.setNewBalance(account.getBalance());
            return vo;
        }
        return null;
    }

    @Override
    public AccountDTO getAccountById(Integer id) {
        Account account = accountMapper.selectAccountById(id);
        if (account!=null){
            //非空返回
            AccountDTO dto=new AccountDTO();
            BeanUtils.copyProperties(account,dto);
            return dto;
        }
        return null;
    }
}







